package cn.lili.controller.user.union;

import cn.lili.common.utils.ResultUtil;
import cn.lili.common.utils.StringUtils;
import cn.lili.common.vo.PageVO;
import cn.lili.common.vo.ResultMessage;
import cn.lili.modules.distribution.entity.dos.Distribution;
import cn.lili.modules.member.entity.dos.UnionMember;
import cn.lili.modules.member.entity.dos.UnionMemberApply;
import cn.lili.modules.member.entity.dos.User;
import cn.lili.modules.member.entity.dto.UnionMemberApplyDTO;
import cn.lili.modules.union.service.UnionMemberApplyService;
import cn.lili.modules.union.service.UnionMemberService;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.ArrayList;
import java.util.List;

@Api(tags = "工会成员管理接口")
@RestController
@RequestMapping("/user/union/join")
@RequiredArgsConstructor
@Validated
public class UnionMemberApplyController {

    private final UnionMemberApplyService unionMemberApplyService;

    @ApiOperation(value = "申请加入工会")
    @PostMapping(value = "/apply")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "unionMemberApplyDTO", value = "申请数据，传值unionId,memberId", required = true, paramType = "query"),
    })
    public ResultMessage<Object> apply(@Valid @RequestBody UnionMemberApplyDTO unionMemberApplyDTO) {
        unionMemberApplyService.apply(unionMemberApplyDTO);
        return ResultUtil.success();
    }

    @ApiOperation(value = "邀请加入工会")
    @PostMapping(value = "/invite")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "unionMemberApplyDTO", value = "邀请数据，传值unionId,memberId", required = true, paramType = "query"),
    })
    public ResultMessage<Object> invite(@Valid @RequestBody UnionMemberApplyDTO unionMemberApplyDTO) {
        unionMemberApplyService.invite(unionMemberApplyDTO);
        return ResultUtil.success();
    }

    @ApiOperation(value = "我的申请分页列表")
    @GetMapping("/myApplyList")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "page", value = "分页对象,含pageNumber(页号),pageSize(每页大小)", required = true, paramType = "query"),
            @ApiImplicitParam(name = "memberId", value = "成员ID", required = true,paramType = "query"),
    })
    public ResultMessage<Page<UnionMemberApply>> getMyApplyByPage(String memberId, PageVO page) {
        return ResultUtil.data(unionMemberApplyService.getMyApplyByPage(memberId,page));
    }


    @ApiOperation(value = "邀请我分页列表")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "page", value = "分页对象,含pageNumber(页号),pageSize(每页大小)", required = true, paramType = "query"),
            @ApiImplicitParam(name = "memberId", value = "成员ID", required = true,paramType = "query"),
    })
    @GetMapping("/inviteMeList")
    public ResultMessage<Page<UnionMemberApply>> getInviteMeByPage(String memberId, PageVO page) {
        return ResultUtil.data(unionMemberApplyService.getInviteMeByPage(memberId, page));
    }


    @ApiOperation(value = "申请审核")
    @GetMapping("/applyAuth")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "recordId", value = "记录ID", required = true, paramType = "query"),
            @ApiImplicitParam(name = "status", value = "审核状态", required = true,
                    allowableValues = "REFUSE(拒绝),PASS(同意)", paramType = "query"),
    })
    public ResultMessage<Object> applyAuth(String recordId,String status) {
        unionMemberApplyService.applyAuth(recordId,status);
        return ResultUtil.success();
    }


    @ApiOperation(value = "邀请审核")
    @GetMapping("/inviteAuth")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "recordId", value = "记录ID", required = true, paramType = "query"),
            @ApiImplicitParam(name = "status", value = "审核状态", required = true,
                    allowableValues = "REFUSE(拒绝),PASS(同意)", paramType = "query"),
    })
    public ResultMessage<Object> inviteAuth(String recordId,String status) {
        unionMemberApplyService.inviteAuth(recordId,status);
        return ResultUtil.success();
    }



    @ApiOperation(value = "申请分页列表")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "page", value = "分页对象,含pageNumber(页号),pageSize(每页大小)", required = true, paramType = "query"),
            @ApiImplicitParam(name = "unionId", value = "工会ID", required = true,paramType = "query"),
            @ApiImplicitParam(name = "status", value = "状态值，取值为PASS或REFUSE或WAIT_APPLY,如果为空，则表示查询全部状态", required = true,paramType = "query"),
            @ApiImplicitParam(name = "optype", value = "类型，取值为APPLY或QUIT,如果为空，则表示查询全部类型", required = true,paramType = "query"),
    })
    @GetMapping("/applyList")
    public ResultMessage<Page<UnionMemberApply>> getApplyByPage(String unionId, PageVO page,String status,String optype) {
        return ResultUtil.data(unionMemberApplyService.getApplyByPage(unionId,page,status,optype));
    }


    @ApiOperation(value = "邀请分页列表")
    @GetMapping("/inviteList")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "page", value = "分页对象,含pageNumber(页号),pageSize(每页大小)", required = true, paramType = "query"),
            @ApiImplicitParam(name = "unionId", value = "工会ID", required = true,paramType = "query"),
    })
    public ResultMessage<Page<UnionMemberApply>> getInviteByPage(String unionId, PageVO page) {
        return ResultUtil.data(unionMemberApplyService.getInviteByPage(unionId, page));
    }


    @ApiOperation(value = "申请退出工会")
    @GetMapping("/quitApply")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "memberId", value = "成员ID", required = true, paramType = "query"),
            @ApiImplicitParam(name = "unionId", value = "工会ID", required = true,paramType = "query"),
    })
    public ResultMessage<Object> quitApply(String memberId,String unionId) {
        unionMemberApplyService.quitApply(memberId,unionId);
        return ResultUtil.success();
    }


    @ApiOperation(value = "退出工会审核")
    @GetMapping("/quitAuth")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "recordId", value = "记录ID", required = true, paramType = "query"),
            @ApiImplicitParam(name = "status", value = "审核状态", required = true,
                    allowableValues = "REFUSE(拒绝),PASS(同意)", paramType = "query"),
    })
    public ResultMessage<Object> quitAuth(String recordId,String status) {
        unionMemberApplyService.quitAuth(recordId,status);
        return ResultUtil.success();
    }



    @ApiOperation(value = "查询退出工会状态，code为1，表示未申请，为200,则有退出状态数据")
    @GetMapping("/myQuitStatus")
    public ResultMessage<UnionMemberApply> myQuitStatus() {
        UnionMemberApply unionMemberApply = unionMemberApplyService.myQuitStatus();
        if(null == unionMemberApply){
            return ResultUtil.error(1,"");
        }
        return ResultUtil.data(unionMemberApplyService.myQuitStatus());
    }


    @ApiOperation(value = "搜索店主")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "keyword", value = "搜索关键词", required = true, paramType = "query"),
    })
    @GetMapping("/getListBy")
    public ResultMessage<List<User>> getListByName (String keyword) {
        if(StringUtils.isNotEmpty(keyword)){
            return ResultUtil.data(unionMemberApplyService.getListByName(keyword));
        }else{
            return ResultUtil.data(new ArrayList<>());
        }

    }

}
